package org.example.controller;

import org.example.domain.Dept;
import org.example.domain.JsonData;
import org.example.domain.Meta;
import org.example.domain.PageBean;
import org.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author Jimmy
 * @version 1.0
 */
@Controller
@ResponseBody
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询
     * @param pagenum 当前页数
     * @param pagesize 每页显示条目数
     * @return json
     */
    @GetMapping(value = "/depts")
    public JsonData<PageBean<Dept>> pageBean(@RequestParam(required = false) String deptId,@RequestParam(required = false) String deptname,@RequestParam(required = false) String pid,String pagenum, String pagesize){
        JsonData<PageBean<Dept>> jsonData = new JsonData<>();
        Meta meta = new Meta();
        int pagenumInt = 0;
        if (pagenum != null && !"".equals(pagenum)){
            pagenumInt = Integer.parseInt(pagenum);
        }
        int pagesizeInt = 0;
        if (pagesize != null && !"".equals(pagesize)){
            pagesizeInt = Integer.parseInt(pagesize);
        }
        PageBean<Dept> pageBean = deptService.findByPage(new Dept(deptId,deptname,pid),pagenumInt, pagesizeInt);
        if (pageBean != null){
            meta.setMsg("获取成功");
            meta.setStatus(200);
        }else {
            meta.setMsg("获取失败");
            meta.setStatus(404);
        }
        jsonData.setData(pageBean);
        jsonData.setMeta(meta);
        return jsonData;
    }

    /**
     * 添加部门信息
     * @param dept dept
     * @return json
     */
    @PostMapping(value = "/depts")
    public JsonData<String> addDept(@RequestBody Dept dept){
        JsonData<String> jsonData = new JsonData<>();
        Meta meta = new Meta();
        int i = deptService.addDept(dept);
        if (i != 0){
            meta.setMsg("添加成功");
            meta.setStatus(200);
        }else {
            meta.setMsg("添加失败");
            meta.setStatus(500);
        }
        jsonData.setMeta(meta);
        return jsonData;
    }

    /**
     * 根据id获取部门信息
     * @param deptId 部门id
     * @return json
     */
    @GetMapping(value = "/getDept/{deptId}")
    public JsonData<Dept> findOneById(@PathVariable(value = "deptId") String deptId){
        Dept dept = deptService.findById(deptId);
        Meta meta;
        if (dept != null){
            meta = new Meta("获取成功",200);
        }else {
            meta = new Meta("获取失败",404);
        }

        return new JsonData<Dept>(dept,meta);
    }

    /**
     * 修改
     * @param dept dept
     * @return json
     */
    @PutMapping(value = "/updateDept/{deptId}")
    public JsonData<String> update(@PathVariable(value = "deptId")String deptId,@RequestBody(required = false) Dept dept){
        dept.setDeptId(deptId);
        int i = deptService.update(dept);
        Meta meta;
        if (i != 0){
            meta = new Meta("修改成功",200);
        }else {
            meta = new Meta("修改失败",404);
        }
        return new JsonData<>(meta);
    }

    /**
     * 删除
     * @param deptId 部门id
     * @return json
     */
    @DeleteMapping(value = "/delDept/{deptId}")
    public JsonData<String> delete(@PathVariable(value = "deptId") String deptId){
        int i = deptService.delete(deptId);
        Meta meta;
        if (i != 0){
            meta = new Meta("删除成功",200);
        }else {
            meta = new Meta("删除失败",404);
        }

        return new JsonData<>(meta);
    }


}
